package com.cs.study.homework._0308;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo2 {
    public static void main(String[] args) {
        //创造线程池
        ExecutorService pool = Executors.newCachedThreadPool();
        //提交任务
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());

    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        Random random = new Random();
        int min = 0;
        int max = 10000;
        int randomInt = random.nextInt(max - min + 1) + min;
        //获取线程开始时间的毫秒值
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(randomInt);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //获取线程结束时间的毫秒值
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "休眠" + (end - start) / 1000 + "秒");
    }
}





